package com.wsc.wisdomchina.activitity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchina.adapter.LivingExpenseAdapter;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.constant.Constant;
import com.wsc.wisdomchina.fragment.BanKuaiFragment;
import com.wsc.wisdomchina.javaBean.HomeModuleBean;
import com.wsc.wisdomchina.javaBean.LivingExpenseBean;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.javaBean.StreetInfoListBean;
import com.wsc.wisdomchina.presenter.ContributorPresenter;
import com.wsc.wisdomchina.view.MyListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 生活缴费列表
 */
public class LivingExpensesListActivity extends BaseActivity implements View.OnClickListener,ContributorView {

    private TextView fragment_lyhd_title;//标题
    private ImageView fragment_lyhd_back;//返回按钮
    private Context context;
    private MyListView myListView;//列表
    private String cidNumber;
    private String projectId;
    private ContributorPresenter contributorPresenter;
    private final String HOMEITEMLISTFLAG="HOMEITEMLISTFLAG";
    private RelativeLayout layout;
    private List<LivingExpenseBean> homeModuleBeanList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.liveexpenselist_layout);
        context=this;
        contributorPresenter = new ContributorPresenter(this, context);
        Intent intent=getIntent();
        cidNumber=intent.getStringExtra("cid");
        projectId=intent.getStringExtra("projectId");
        initView();
    }

    private void initView() {
        fragment_lyhd_title=(TextView) findViewById(R.id.tv_headtitle);//标题
        fragment_lyhd_back=(ImageView) findViewById(R.id.regist_back);//返回按钮
        fragment_lyhd_back.setOnClickListener(this);
        fragment_lyhd_title.setText("机构选择");
        myListView=(MyListView) findViewById(R.id.fragment_bxjdxq_listView);//列表
        myListView.setDivider(null);
        contributorPresenter.getHomeItemList(projectId,cidNumber,HOMEITEMLISTFLAG);
        layout=(RelativeLayout) findViewById(R.id.layout);
        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(context,LivingExpensesCountActivity.class);
                intent.putExtra("projectId",projectId);
                intent.putExtra("itemId",homeModuleBeanList.get(position).getItemId());
                intent.putExtra("cidNumber",cidNumber);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            //banner
            if (flag.equals(HOMEITEMLISTFLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    homeModuleBeanList=JsonTools.jsonStringToList(jsonMessage,LivingExpenseBean.class);
                    if(homeModuleBeanList!=null && homeModuleBeanList.size()>0)
                    {
                        layout.setVisibility(View.GONE);
                        LivingExpenseAdapter livingExpenseAdapter = new LivingExpenseAdapter(context, homeModuleBeanList);
                        myListView.setAdapter(livingExpenseAdapter);
                    }
                }else
                {
                    layout.setVisibility(View.VISIBLE);
                }

            }

        } catch (Exception e) {

        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
    }


    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.regist_back:
                finish();
                break;
        }
    }

}
